package com.hzspace.single.repository;

import com.hzspace.single.bean.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2020/3/5_14:44
 */
public interface BookRepository extends JpaRepository<Book, Integer> {

    List<Book> findAllByIdGreaterThanEqual(Integer id);

    List<Book> findAllByIdLessThanEqualOrNameContaining(Integer id, String name);

    List<Book> findAllByAuthorContains(String author);

    @Query(value = "select * from book where id=(select max(id) from book)", nativeQuery = true)
    Book maxIdBook();

    //参数顺序要一致 name是?1 author对应?2
    @Query(value = "insert into book (name,author) values (?1,?2);", nativeQuery = true)
    @Modifying
    @Transactional
    Integer insertBook(String name, String author);

    //不需要参数顺序一致
    @Query(value = "insert into book (name,author) values (:name,:author);", nativeQuery = true)
    @Modifying
    @Transactional
    Integer insertBook2(@Param("name") String name, @Param("author") String author);
}
